﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using InTheHand.Net;
using Brecham.Obex;
using Brecham.Obex.Objects;

namespace NuiBlue.Bluetooth
{
    public class SessionHolder
    {
        public ObexClientSession session { get; private set; }
        public BluetoothClient client { get; private set; }

        public SessionHolder(BluetoothAddress address, string pin)
        {
            BluetoothEndPoint endpointAsync;
            client = new BluetoothClient();

            BluetoothWin32Authentication auth = new BluetoothWin32Authentication(address, pin);

            endpointAsync = new BluetoothEndPoint(address, BluetoothService.ObexFileTransfer);

            client.SetPin(pin);
            try
            {
                client.Connect(endpointAsync);

                session = new ObexClientSession(client.GetStream(), UInt16.MaxValue);
                session.Connect(ObexConstant.Target.FolderBrowsing);
            }
            catch (Exception be) { }
        }

        public void closeConnection()
        {
            if (session != null)
            {
                session.Disconnect();
                session.Dispose();
            }
            if (client != null)
            {
                client.Close();
            }
        }
    }
}
